﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HotelManagement.Common;
using HotelManagement.BL;

namespace HotelManagement.PL
{
    public partial class UserMngForm : Form
    {
        public UserMngForm()
        {
            InitializeComponent();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int nSelectRow = CheckChoose();
            if (nSelectRow < 0)
            {
                return;
            }
            UserEditForm userMngForm = new UserEditForm(true);
            userMngForm.UserInfo = buildUserInfo(nSelectRow);
            userMngForm.ShowDialog(this);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            UpdateGridView();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            UserEditForm userMngForm = new UserEditForm(false);
            userMngForm.ShowDialog(this);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int nSelectRow = CheckChoose();
            if (nSelectRow < 0)
            {
                return;
            }

            if (DeleteGoods(buildUserInfo(nSelectRow)))
            {
                MessageBox.Show("操作成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                UpdateGridView();
            }
            else
            {
                MessageBox.Show("操作失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private bool DeleteGoods(User user)
        {
            bool bReturn = false;
            UserBL userMng = new UserBL();
            try
            {
                bReturn = userMng.DeleteUser(user) > 0 ? true : false;
            }
            catch
            {

            }

            return bReturn;
        }

        internal void UpdateGridView()
        {
            int nResult;
            try
            {
                User userInfo = new User();
                userInfo.Name = txtName.Text.Trim();

                UserBL userBL = new UserBL();
                nResult = userBL.Search(ref userInfo);

                if (userInfo.UserCollection != null)
                {
                    dgvGoods.AutoGenerateColumns = false;
                    dgvGoods.DataSource = userInfo.UserCollection;
                }
            }
            catch
            {

            }
        }

        private int CheckChoose()
        {
            int nCount = 0;
            int nRowIndex = -1;
            foreach (DataGridViewRow dgr in dgvGoods.Rows)
            {
                if (dgr.Cells["choose"].Value != null && dgr.Cells["choose"].Value.Equals(true))
                {
                    nCount++;
                    nRowIndex = dgr.Index;
                }
            }

            if (nCount == 0)
            {
                MessageBox.Show("请选择用户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                nRowIndex = -1;
            }

            if (nCount > 1)
            {
                MessageBox.Show("只能选择一个", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                nRowIndex = -1;
            }

            return nRowIndex;
        }

        private User buildUserInfo(int rowIndex)
        {
            User user = new User();
            user.ID = dgvGoods.Rows[rowIndex].Cells["ID"].Value.ToString();
            user.Name = dgvGoods.Rows[rowIndex].Cells["USERNAME"].Value.ToString();
            user.Password = dgvGoods.Rows[rowIndex].Cells["PASSWORD"].Value.ToString();
            return user;
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {
            UpdateGridView();
        }

        

        
    }
}
